package com.playcrab.adouban;

import com.google.gdata.client.douban.DoubanService;
import com.playcrab.adouban.util.Constants;
import com.playcrab.adouban.util.DoubanAPI;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	protected String userId;

	protected View pendingView;
	protected View panelView;
	public void checkLoggedin(){ 
		if (!isLoggedin()){
			setTitle(this.getString(R.string.app_name) );
			showAuthDialog();
			return;
		}
		else {  
			this.userId = this.getStoredUserId();
			setTitle(this.userId.concat("@").concat(this.getString(R.string.app_name)));
			return;
		}
	}
	
	public void viewUser(String uid){
		Intent intent = new Intent(this, User.class); 
		intent.putExtra("id", uid ); 
		intent.putExtra("type", "user" );
		startActivity(intent);  
	}
	
	protected View InsertPanel() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View panel = inflater.inflate(R.layout.panel, null);
		LayoutParams params = new TableLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		this.addContentView(panel, params);
		
		// miniblog
		Button miniblogButton = (Button) panel.findViewById(R.id.miniblog_button);

		miniblogButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, MiniblogList.class);
				startActivity(intent);
			}
		});

		// collection
		Button collectionButton = (Button) panel.findViewById(R.id.collection_button);
		collectionButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, Collection.class);
				intent.putExtra("type", "book");
				startActivity(intent);
			}
		});

		// scan
		Button scanButton = (Button) panel.findViewById(R.id.scan_button);
		scanButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Intent intent = new Intent(
							"com.google.zxing.client.android.SCAN");
					startActivityForResult(intent, 0);
				} catch (ActivityNotFoundException e) {
					BaseActivity.this.showInfo(BaseActivity.this
							.getString(R.string.barcode_error));

					e.printStackTrace();
				}
				return;
			}
		});

		//doumail
		Button doumailButton = (Button) panel.findViewById(R.id.doumail_button); 
		doumailButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, 
						 Doumail.class);
				intent.putExtra("type", "inbox");
				startActivity(intent);
			}
		});
		
		//friends
		Button friendsButton = (Button) panel.findViewById(R.id.friends_button); 
		friendsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, 
						 User.class);
				BaseActivity.this.checkLoggedin();
				String userId = BaseActivity.this.getStoredUserId();
				intent.putExtra("id", userId);
				intent.putExtra("type", "friends");
				startActivity(intent);
			}
		});
		
		//contacts
		Button contactsButton = (Button) panel.findViewById(R.id.contacts_button); 
		contactsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, 
						 User.class);
				BaseActivity.this.checkLoggedin();
				String userId = BaseActivity.this.getStoredUserId();
				intent.putExtra("id", userId);
				intent.putExtra("type", "contacts");
				startActivity(intent);
			}
		});
		
		panelView = panel;
		return (panel);
	}

	protected View showPending() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View row = inflater.inflate(R.layout.autorefresh_row, null);
		LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		this.addContentView(row, params);
		View child = row.findViewById(R.id.throbber);
		child.setVisibility(View.VISIBLE);
		RotateAnimation rotate = new RotateAnimation(0f, 360f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f); 
		rotate.setDuration(600);
		rotate.setRepeatMode(Animation.RESTART);
		rotate.setRepeatCount(Animation.INFINITE);
		child.startAnimation(rotate);

		pendingView = row;
		return (row);
	}

	protected void hidePending() {
		View child = pendingView.findViewById(R.id.throbber);
		child.setVisibility(View.GONE);
		child.clearAnimation();
	}
	public DoubanService getAuthedDoubanService() {

		DoubanService myService = DoubanAPI.getInstance().getService();
		SharedPreferences settings = getSharedPreferences(
				Constants.PREF_DOUBAN, 0);
		if(acessTokenIsValid()){
			String acessToken = settings.getString(Constants.ACCESS_TOKEN,
			"false");
			String acessTokenSecret = settings.getString(
					Constants.ACCESS_TOKEN_SECRET, "false");
			myService.setAccessToken(acessToken, acessTokenSecret);
			return myService;
		}else{
			showAuthDialog();
			return null;
		}

		
	}
	
	public String getStoredUserId(){
		SharedPreferences settings = getSharedPreferences(
				Constants.PREF_DOUBAN, 0);
		String userId = settings.getString(Constants.USER_NAME, "kevinye");
		return userId;
	}
	public boolean isLoggedin(){
		SharedPreferences settings = getSharedPreferences(
				Constants.PREF_DOUBAN, 0);
		String requestToken = settings.getString(Constants.ACCESS_TOKEN,
				"false");
		String userId = settings.getString(Constants.USER_NAME, null);
		if (requestToken.equals("false") ||   userId == null )
			return false;
		else
			return true; 
	}
	
	public boolean acessTokenIsValid() {
		SharedPreferences settings = getSharedPreferences(
				Constants.PREF_DOUBAN, 0);
		String requestToken = settings.getString(Constants.ACCESS_TOKEN,
				"false"); 
		if (requestToken.equals("false")   )
			return false;
		else
			return true;
	}
	
	
	public void showAuthDialog() {
		Intent intent = new Intent(this,
				Auth.class);
		startActivity(intent); 
	}

	public void showAuthDialog(Context cnt) {
		Intent intent = new Intent(cnt,Auth.class);
		startActivity(intent); 
	}
	
	public void showInfo(String info){ 
		Intent intent = new Intent(BaseActivity.this,
				ShowInfo.class);
		intent.putExtra("info", info);
		startActivity(intent); 
	}

	 

	protected void openWebBrowser(String url) {
		Log
				.i(this.getClass().getName(), "about to launch browser, url: "
						+ url);
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}
	

}
